package com.welink.cmp.statemachine.machine;

import com.welink.base.statemachine.StateMachine;
import com.welink.base.statemachine.builder.StateMachineBuilder;
import com.welink.base.statemachine.builder.StateMachineBuilderFactory;
import com.welink.cmp.statemachine.StateMachineIdContent;
import com.welink.cmp.statemachine.actions.CommonActions;
import com.welink.cmp.statemachine.context.CommonCtx;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author yanwei li create  2024-06-02 22:58
 */
@Component
@Slf4j
public class CommonMachine {

    @Autowired
    private CommonActions commonActions;

    @PostConstruct
    public void init(){
        final StateMachineBuilder<CommonState, CommonEvent, CommonCtx> builder = StateMachineBuilderFactory.create();

        builder.addTransition()
                .from(CommonState.NEW)
                .to(CommonState.IDLE)
                .on(CommonEvent.MOUNTED)
                .perform(commonActions.doAction());

        builder.addTransition()
                .from(CommonState.IDLE)
                .to(CommonState.ACTIVE)
                .on(CommonEvent.ALLOCATED)
                .perform(commonActions.doAction());
        // 下架防火墙 空闲-> 创建
        builder.addTransition()
                .from(CommonState.IDLE)
                .to(CommonState.NEW)
                .on(CommonEvent.UNMOUNTED)
                .perform(commonActions.doAction());

//        builder.addTransition()
//                .from(CommonState.NEW)
//                .to(CommonState.RETIRED)
//                .on(CommonEvent.RECYCLED)
//                .perform(doAction());

        final StateMachine<CommonState, CommonEvent, CommonCtx> build = builder.build(StateMachineIdContent.common_state_machine_id);
        log.info("初始化通用状态机已完成 successful ··· ···");
    }

}